/*
 * Created on 15.01.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.obj.OrgType;
import ro.gateway.aida.obj.db.OrgTypeDB;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.obj.db.PersonDB;
import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.struts.form.OrgPersSearchForm;
import ro.gateway.aida.utils.LocalisedSortableList;
import ro.gateway.aida.utils.PageToken;
import ro.gateway.aida.utils.SortedList;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu<p>
 *
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 */
public class OrgPersSearchAction extends MultiTilesAction {
	private PersonDB pdb;
	private OrganizationDB odb;
	private OrgTypeDB otdb;

    public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {


        pdb=PersonDB.getManager(token);
        odb=OrganizationDB.getManager(token);
        otdb=OrgTypeDB.getManager(token);
    	HttpSession session = request.getSession();
    	String lang = TranslatorUtils.getCurrentLanguage(request, sc)
		 .getLanguage();

		if (session.getAttribute(OrgType.LIST) == null)
			session.setAttribute(OrgType.LIST, otdb.getAllLocalized(lang));
		else {
		
			((LocalisedSortableList)session.getAttribute(OrgType.LIST)).setViewLang(lang);
		}
        

        return modeSelect(mapping, form, request, response);
    }

    
    public ActionForward modeSearchPersons(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        	OrgPersSearchForm f=(OrgPersSearchForm)form;
        	String lang = TranslatorUtils.getCurrentLanguage(request, sc)
    		 .getLanguage();
        	HttpSession session = request.getSession();
        	session.removeAttribute(ORG_LIST);
        	session.setAttribute(PERSON_LIST,pdb.selectorSearch(f.getPersonText(),f.getPersonKey(),lang));
        	return modeViewPersons(mapping,form,request,response);
        }
        


    public ActionForward modeSearchOrgs(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    	OrgPersSearchForm f=(OrgPersSearchForm)form;
    	String lang = TranslatorUtils.getCurrentLanguage(request, sc)
		 .getLanguage();
    	HttpSession session = request.getSession();
    	session.removeAttribute(PERSON_LIST);
    	session.setAttribute(ORG_LIST,odb.directorySearch(f.getOrgText(),f.getType(),f.getOrgKey()));
    	return modeViewOrgs(mapping,form,request,response);
    }
    	
    public ActionForward modeViewPersons(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            HttpSession session = request.getSession();
        	String lang = TranslatorUtils.getCurrentLanguage(request, sc)
    		 .getLanguage();
            SortedList l = (SortedList) session.getAttribute(PERSON_LIST);
             PageToken.tokenizeList( request,  sc,  l, "persons");
             Utils.setBreadCrumbs(request,"bc.orgpers.personlist");
            return mapping.findForward("person.list");
        }
    
    
    public ActionForward modeViewOrgs(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();
    	String lang = TranslatorUtils.getCurrentLanguage(request, sc)
		 .getLanguage();
        LocalisedSortableList l = (LocalisedSortableList) session.getAttribute(ORG_LIST);
        if (!lang.equals(l.getViewLang())) l.setViewLang(lang);
         PageToken.tokenizeList( request,  sc,  l, "orgs");
         Utils.setBreadCrumbs(request,"bc.orgpers.orglist");
        return mapping.findForward("org.list");
    }

    public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

        if (request.getParameter("orgSearch")!=null)
            return modeSearchOrgs(mapping, form, request, response);
        if (request.getParameter("personSearch")!=null)
            return modeSearchPersons(mapping, form, request, response);        
        HttpSession session = request.getSession();
        if(request.getParameter("start")!=null) 
        	if (session.getAttribute(ORG_LIST)!=null)
        	return modeViewOrgs(mapping,form,request,response);
        	else
        	return modeViewPersons(mapping,form,request,response);

        Utils.setBreadCrumbs(request,"bc.orgpers");
        return mapping.getInputForward();
    }

    public static final String ORG_LIST = "Directory_OrgSearch_LIST";
    public static final String PERSON_LIST = "Directory_PersonSearch_LIST";
}
